home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Games / MoofWars / MoofWars Encoder 8⁄15⁄96 / •Sources / PICTEncode.cp < prev    next >
Encoding:
Text File  |  1996-08-19  |  3.1 KB  |  116 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    PICTEncode.cp
  4.  
  5. #
  6. #    Author: Timothy Carroll
  7. #    Apple Developer Technical Support
  8. #    timc@apple.com
  9. #
  10. #    Modification History: 
  11. #
  12. #    8/15/96        TMC     Initial Release
  13. #
  14. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  15. #
  16. #
  17. #    You may incorporate this sample code into your applications without
  18. #    restriction, though the sample code has been provided "AS IS" and the
  19. #    responsibility for its operation is 100% yours.  However, what you are
  20. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  21. #    after having made changes. If you're going to re-distribute the source,
  22. #    we require that you make it clear in the source that the code was
  23. #    descended from Apple Sample Code, but that you've made changes.
  24. #
  25. *************************************************************************************/
  26.  
  27. #include "TGraphic.h"
  28. #include "PictEncode.h"
  29.  
  30. OSStatus PICTEncode (short inputFileResNum, short outputFileResNum)
  31. {
  32.     OSStatus        theErr;
  33.  
  34.     UInt16            numPicts, loop;
  35.  
  36.     // we pass these to GetResInfo so that we can get the actual resource ID.
  37.     SInt16            resID;
  38.     ResType            resType;
  39.     Str255            resName;
  40.  
  41.     // Holds the last value on the resource chain since we change the top resource a lot
  42.     SInt16            saveResNum;
  43.  
  44.     // Temporarily holds the resource so we can get information on it
  45.     Handle             pict = NULL;
  46.     TGraphic         *compiled = NULL;
  47.         
  48.     saveResNum = CurResFile();
  49.  
  50.     UseResFile (inputFileResNum);
  51.  
  52.  
  53.     // First thing is to copy the color table resource to the output.
  54.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  55.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  56.  
  57.  
  58.     // get the color table
  59.     gAppColorTable = GetCTable( kAppColorTableResID );
  60.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  61.  
  62.     // determine the number of PICT resources
  63.     numPicts = Count1Resources( 'PICT' );
  64.  
  65.     
  66.     // get each one,
  67.     for( loop = 1; loop <= numPicts; loop++ )
  68.     {
  69.         // load the pict
  70.         UseResFile (inputFileResNum);
  71.                     
  72.         // we'll get the pict first so that we can get the information out of it.
  73.         pict = Get1IndResource( 'PICT', loop );
  74.         theErr = ResError();
  75.         FAIL_NIL (pict, "\pFailed to load the picture")
  76.         FAIL_OSERR (theErr, "\pFailed to load the picture")
  77.             
  78.         // determine its id and name
  79.         GetResInfo( pict, &resID, &resType, resName );
  80.         theErr = ResError();
  81.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  82.         
  83.         ReleaseResource (pict);
  84.         
  85.         // Okay, we know the PICT's resID, build the compiled graphic and write it to the output file.
  86.         compiled = TGraphic::NewGraphic (resID);
  87.         FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
  88.         
  89.         UseResFile (outputFileResNum);
  90.         
  91.         compiled->WriteToGraphicResource();
  92.         compiled->WriteToPICTResource();
  93.         compiled->DisposeReference();
  94.  
  95.     }
  96.     // restore the previous port and device
  97.     
  98.     goto cleanup;
  99.     
  100. error:
  101.     
  102.     if (theErr == noErr)
  103.         theErr = paramErr;
  104. cleanup:
  105.     
  106.     UseResFile (saveResNum);
  107.     
  108.     if (pict)
  109.         ReleaseResource (pict);
  110.     if (compiled)
  111.         compiled->DisposeReference();
  112.     if (gAppColorTable != NULL)
  113.         DisposeCTable (gAppColorTable);
  114.     gAppColorTable = NULL;
  115.     return theErr;
  116. }